/*

*/

client
	control_freak=1
	Del()
		if(isplayer(mob))
			var/player/p = mob
			if(p.slot) p.save(p.slot)
		..()

	proc
		change_windows()
			winshow(src, "characters", 0)
			winshow(src, "default")

		load(slot)
			var/savefile/f = new /savefile(path+"char[slot]/character.sav")
			f >> mob
			change_windows()
			if(mob.BOOK)
				var/player/p = mob
				p.draw_hotkeys()

			if(mob.hp.at_min())
				mob.death(null)

		update_player_names(slot, name)
			players[slot] = name
			save_character_names()
			draw_char_buttons()

		save_character_names()
			if(fexists(path+"names.txt")) fdel(path+"names.txt")
			for(var/o in players)
				text2file(o, path+"names.txt")

		char_error(err)
			winset(src, "create_character.error","text=\"[err]\"")

		draw_char_buttons()
			for(var/x=1 to MAX_CHARS)
				if(winexists(src, "characters.pbutton[x]")) winset(src, "pbutton[x]","parent=null")

				var/list/newbutton = new /list()

				newbutton["type"] = "button"

				newbutton["pos"] = "0,[32*(x-1)]"

				newbutton["is-visible"] = "true"

				newbutton["parent"] = "characters"

				newbutton["text"] = players[x]

				newbutton["size"] = "243x32"
				newbutton["command"] = "open-player-menu [x]"

				winset(src, "pbutton[x]", list2params(newbutton))

		create_player_labels(slot)
			//see #+characters.txt.dm for the "magic numbers"
			var/list/stats = dd_file2list(path+"char[slot]/character.txt")
			if(stats.len < 4) return

			var/list/other_button = new /list()

			other_button["type"] = "label"
			other_button["pos"] = "243,32"
			other_button["is-visible"] = "true"
			other_button["parent"] = "characters"
			other_button["size"] = "100x64"
			other_button["align"] = "left"
			other_button["text"] = "Class:\nLevel\nHP:"
			winset(src, "levellabel", list2params(other_button))

			other_button["type"] = "label"
			other_button["pos"] = "343,32"
			other_button["is-visible"] = "true"
			other_button["parent"] = "characters"
			other_button["size"] = "243x64"
			other_button["align"] = "left"
			other_button["text"] = "[capitalize(stats[2])]\n[stats[3]]\n[stats[4]]"
			winset(src, "statlabel", list2params(other_button))

	verb
		open_player_menu(slot as num)
			set instant=1
			winset(src, "characters","size=486x320")
			if(winexists(src, "characters.delbutton")) winset(src, "characters.delbutton","parent=null")
			if(winexists(src, "characters.newbutton")) winset(src, "characters.newbutton","parent=null")
			if(winexists(src, "characters.levellabel")) winset(src, "characters.levellabel","parent=null")
			if(winexists(src, "characters.statlabel")) winset(src, "characters.statlabel","parent=null")

			var/list/newbutton = new /list()

			newbutton["type"] = "button"

			newbutton["pos"] = "243,0"

			newbutton["is-visible"] = "true"

			newbutton["parent"] = "characters"
			newbutton["size"] = "243x32"

			if(players[slot]!="New Character")
				newbutton["text"] = "Play [players[slot]]"

				newbutton["command"] = "play-character [slot]"

				var/list/other_button = new /list()
				other_button["type"] = "button"
				other_button["pos"] = "243,288"
				other_button["is-visible"] = "true"
				other_button["parent"] = "characters"
				other_button["size"] = "243x32"
				other_button["text"] = "Delete [players[slot]]"
				other_button["command"] = "delete-character [slot]"
				winset(src, "delbutton", list2params(other_button))

				create_player_labels(slot)

			else
				newbutton["text"] = "Create [players[slot]]"

				newbutton["command"] = "open-new-character [slot]"

			winset(src, "newbutton", list2params(newbutton))

		open_new_character(slot as num)
			winset(src, "create_character.create","command=\"create-character [slot]\"")
			char_error("")
			change_class()
			winset(src, "characters","size=662x320")
			var/list/newpane = new /list()
			newpane["type"] = "child"
			newpane["pos"] = "486,0"
			newpane["is-visible"] = "true"
			newpane["parent"] = "characters"
			newpane["size"] = "176x320"
			newpane["left"] = "create_character"
			winset(src, "charpane",list2params(newpane))

		play_character(slot as num)
			if(!fexists(path+"char[slot]/character.sav"))
				if(!fexists(path+"char[slot]/character.txt")) return
				var/player/p = new /player
				change_windows()
				mob = p
				mob.sight &= ~BLIND
				p.set_up_character(slot)
				p.save(slot)
			else
				load(slot)

		delete_character(slot as num)
			if(alert(src, "Are you sure you want to delete [players[slot]]?","Deleting a Character","Yes","No")=="Yes")
				fdel(path+"char[slot]/")
				update_player_names(slot, "New Character")
				winset(src, "characters","size=243x320")

		change_class()
			switch(get_radio_selection(src, "create_character", "buttons"))
				if("magical")	winset(src, "create_character.description","text=\"[magical_text]\"")
				if("support")	winset(src, "create_character.description","text=\"[support_text]\"")
				if("restore")	winset(src, "create_character.description","text=\"[restore_text]\"")
				if("ranged")	winset(src, "create_character.description","text=\"[ranged_text]\"")

		create_character(slot as num)

			var
				class = get_radio_selection(src, "create_character", "buttons")
				name  = capitalize(make_proper(winget(src, "create_character.name", "text")))

			if(!name) { char_error("Please input a name.") ; return }
			if(!class) { char_error("Please choose a class.") ; return }

			winset(src, "characters.charpane","parent=null")
			winset(src, "characters","size=243x320")

			if(fexists(path+"char[slot]/character.txt"))fdel(path+"char[slot]/character.txt")
			text2file("[name]\n[class]\n0\n100/100", path+"char[slot]/character.txt")

			update_player_names(slot, name)

			draw_char_buttons()

		show_window(win as text)
			winshow(src, win)

var
	restore_text = {"
	Restorative monks are the type of monk who's specialized in healing.
	Bonuses:
	Sustain
	Spell Regeneration
	"}
	support_text = {"
	Support monks are specialized in status boost mantra.
	Bonuses:
	Speed
	Buff Duration
	"}
	magical_text = {"
	Magical monks are like the damage-type of monk. They have an all-around improvement on damage.
	Bonuses:
	Power
	Armor Penetration
	"}
	ranged_text  = {"
	Ranged monks are a special type of monk, who have very far reaching skills, and incredible fortitude.
	Bonuses:
	Range
	HP Boost
	"}